import os   
from dbentries import *
from controls.user_control import *
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

class MainPage(webapp.RequestHandler):
    def get(self):
        template_values = { } 
        path = os.path.join(os.path.dirname(__file__), 'index.html') 
        self.response.out.write(template.render(path, template_values))
        #user = users.get_current_user()
        #if user:
        #    userInfo = UserControl().get_end_user(user)
        #    if userInfo == None:
        #        self.redirect("/newentry")
        #    else:
        #        self.redirect("/showitem")
        #else:
        #    self.redirect(users.create_login_url(self.request.uri))"""

class NewEntry(webapp.RequestHandler):
    def get(self): 
        user = users.get_current_user()
        if user:
            template_values = { 'username': user.nickname(),} 
            path = os.path.join(os.path.dirname(__file__), 'newuser.html') 
            self.response.out.write(template.render(path, template_values))
        else:
            self.redirect(users.create_login_url(self.request.uri))

class CreateUserSuccessful(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            if UserControl().add_user(user,
                            self.request.get("phone"),
                            self.request.get("category"),
                            self.request.get("description")) == None:
                self.response.out.write("Create user failed!")
            else:
                self.response.out.write("Create user successfully!")

        else:
            self.redirect(users.create_login_url(self.request.uri))

class EditEntry(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write("Welcome, :1" + user.nickname())
        else:
            self.redirect(users.create_login_url(self.request.uri))

class DeleteEntry(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write("Welcome, :1" + user.nickname())
        else:
            self.redirect(users.create_login_url(self.request.uri))

class ShowEntry(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            userInfo = UserControl().get_end_user(user)
            """money_items = MoneyControl().get_item_by_user(userinfo.key())
            if money_items:
                template_values = { 'username': user.nickname(),'money_items': money_items} 
                path = os.path.join(os.path.dirname(__file__), 'showinfo.html') 
                self.response.out.write(template.render(path, template_values))"""
        else:
            self.redirect(users.create_login_url(self.request.uri))

application = webapp.WSGIApplication([('/', MainPage),
                                     ('/newentry', NewEntry),
                                     ('/showitem', ShowEntry),
                                     ('/editentry', EditEntry),
                                     ('/deleteentry', DeleteEntry),
                                     ('/createusersuccessful', CreateUserSuccessful),
                                     ],
                                     debug=True)

def main():
    run_wsgi_app(application)
    pass
if __name__ == '__main__':
    main()
